 package com.chrilwe.pigpig.auth.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.chrilwe.pigpig.auth.common.request.CheckCodeRequest;
import com.chrilwe.pigpig.auth.common.response.ResultEnum;
import com.chrilwe.pigpig.auth.common.response.ValidateCodeResult;
import com.chrilwe.pigpig.auth.service.PhoneService;
import com.chrilwe.pigpig.auth.service.ValidateCodeService;

import cn.chrilwe.pigpig.core.base.BaseController;
import cn.chrilwe.pigpig.core.response.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * 验证码模块
 * 
 * @author chrilwe 2020-8-24
 */
@RestController
@RequestMapping("/validateCode")
@Api(tags = "验证码生成接口")
public class ValidateCodeController extends BaseController {

	@Autowired
	protected ValidateCodeService validateCodeService;
	
	@Autowired
	protected PhoneService phoneService;

	@GetMapping("/get")
	@ApiOperation("密码登录模式验证码获取")
	public void passwordLoginValidateCode(@RequestParam("clientToken") String clientToken) {
		// 设置浏览器不缓存本页
		response.addHeader("Pragma", "no-cache");
		response.addHeader("Cache-Control", "no-cache");
		response.addHeader("Expires", "0");

		BufferedImage codeImage = validateCodeService.getCodeImage(clientToken);
		response.setContentType("image/jpeg");
		
		try {
			ImageIO.write(codeImage, "jpeg", response.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@ApiOperation("九宫格图片获取")
	@GetMapping("/images")
	@ApiImplicitParam(name = "clientId", value = "客户端唯一ID标识")
	public ValidateCodeResult validateCodeImages(@RequestParam("clientId")String clientId) {
		
		return validateCodeService.getCodeImageUrls(clientId);
	}
	
	@PostMapping("/check")
	@ApiOperation("校验九宫格图片选择是否正确")
	public BaseResult checkCode(@RequestBody CheckCodeRequest codeRequest) {
		String clientId = codeRequest.getClientId();
		String imageIds = codeRequest.getImageIds();
		boolean checkCode = validateCodeService.checkCode("tmp"+clientId, imageIds);
		if(checkCode) 
			return new BaseResult(ResultEnum.SUCCESS.getCode(),ResultEnum.SUCCESS.getName());
		else
			return new BaseResult(ResultEnum.ERR.getCode(),ResultEnum.ERR.getName());
	}
	
	@PostMapping("/sendPhoneCode")
	@ApiOperation("发送手机验证码")
	@ApiImplicitParam(name = "phone", value = "手机号码")
	public BaseResult sendPhoneCode(@RequestBody String phone) {
		boolean res = phoneService.doSendPhoneCode(phone);
		if(res) {
			return new BaseResult(ResultEnum.SUCCESS.getCode(),ResultEnum.SUCCESS.getName());
		}
		return new BaseResult(ResultEnum.UNKNOW_ERR.getCode(),ResultEnum.UNKNOW_ERR.getName());
	}
}
